package com.demo.controller;

import com.demo.common.Result;
import com.demo.pojo.dto.CourseListDTO;
import com.demo.pojo.entity.Course;
import com.demo.service.CourseService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 课程管理控制器
 * Author：潘建冰
 * Package：com.demo.controller
 * Project：demo
 * name：CourseController
 * Date：2025/9/11  17:20
 */
@RestController
@RequestMapping("/course")
@Validated
public class CourseController {

    private final CourseService courseService;

    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }

    /**
     * 根据id查询课程数据
     * @param id 课程id
     * @return Result
     */
    @GetMapping("/select")
    public Result<Course> select(@RequestParam Long id){
        return Result.success(courseService.selectById(id));
    }

    /**
     * 保存课程数据
     * @param course 课程数据
     * @return  Result
     */
    @PostMapping("/save")
    public Result<Void> save(@Valid @RequestBody Course course){
        courseService.saveCourse(course);
        return Result.success();
    }

    /**
     * 修改课程数据
     * @param course 课程数据
     * @return  Result
     */
    @PutMapping("/update")
    public Result<Void> update(@Valid @RequestBody Course course){
        courseService.updateCourse(course);
        return Result.success();
    }

    /**
     * 删除课程数据
     * @param id 课程id
     * @return  Result
     */
    @DeleteMapping("/delete")
    public Result<Void> delete(@RequestParam Long id){
        courseService.removeById(id);
        return Result.success();
    }

    /**
     * 分页查询课程数据
     * @param pageNum 页码
     * @param pageSize 每页数据量
     * @param coName 课程名称
     * @param coCredit 课程学分
     * @param coType 课程类型
     * @param coRemark 备注
     * @return  Result
     */
    @GetMapping("/listPage")
    public Result<Map<String, Object>> listPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam(required = false) String coName,
                                @RequestParam(required = false) Float coCredit,
                                @RequestParam(required = false) String coType,
                                @RequestParam(required = false) String coRemark){
        //封装DTO对象进行传递
        CourseListDTO courseListDTO = new CourseListDTO();
        courseListDTO.setCoName(coName);
        courseListDTO.setCoCredit(coCredit);
        courseListDTO.setCoType(coType);
        courseListDTO.setCoRemark(coRemark);
        courseListDTO.setPageNum(pageNum);
        courseListDTO.setPageSize(pageSize);
        return Result.success(courseService.listPage(courseListDTO));
    }

    /**
     * 获取所有课程列表
     * @return Result
     */
    @GetMapping("/list")
    public Result<List<CourseListDTO>> getCourseList(){
        return Result.success(courseService.getCourseList());
    }
}


